package com.example.a10.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.a10.mapper.CosManager;
import com.example.a10.model.dto.upload.UploadRequest;
import com.example.a10.newentity.testFile;
import com.example.a10.service.FileService;
import com.example.a10.service.testFileService;
import com.example.a10.util.DocumentUtil;
import com.example.a10.util.FileUploadBizEnum;
import com.example.a10.util.UUIDUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/Test")
public class TestCintroller {

    @Autowired
    FileService fileService;
    @Autowired
    testFileService testFileService;
    @Autowired
    CosManager cosmanager;

    @ApiOperation("上传测试文件,并返回所有测试文件的所有名称")
    @PostMapping("/Upload")
    @Transactional(rollbackFor = Exception.class) // 添加事务注解
    public String TestUpload(@ModelAttribute UploadRequest uploadRequest) throws Exception {

        String userid = uploadRequest.getUserid();
        MultipartFile file = uploadRequest.getFile();
        //1.将数据存进云服务器
            // 选择上传文件的业务类型
            FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.Test;
            // 调用上传文件方法
            String Url = fileService.uploadFile(file, fileUploadBizEnum);

            if(Url!=null){
                //2.存进数据库
                testFile testFile = new testFile();
                //生成id
                String testId = UUIDUtil.getUUID();
                // 获取文件名
                String name = file.getOriginalFilename();
                //文件大小
                String filesize = DocumentUtil.getFileSize(file.getSize());
                testFile.setId(testId);
                testFile.setFileName(name);
                testFile.setFileSize(filesize);
                testFile.setCreateTime(LocalDateTime.now());
                testFile.setUrl(Url);
                testFile.setUserId(userid);
                testFileService.save(testFile);
                return "success";
            } else {
            throw new Exception();
        }
    }

    @ApiOperation("返回所有训练文件的所有名称")
    @GetMapping("/Testlist")
    @ResponseBody
    public List<testFile> selectTestlist(String userid){
        LambdaQueryWrapper<testFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(testFile::getUserId,userid);
        List<testFile> testFileList = testFileService.list(lambdaQueryWrapper);
        return testFileList;
    }

    @ApiOperation("返回测试文件名称，删除模型")
    @DeleteMapping("/delete")
    @Transactional(rollbackFor = Exception.class) // 添加事务注解
    public String deleteTest(@RequestBody testFile testFile){
        if(testFile!=null){
            String testFileId = testFile.getId();
            String url = testFile.getUrl();
            testFileService.removeById(testFileId);
            try {
                cosmanager.deleteFile(url);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return "success";
        }else {
            return "failed";
        }

    }


}



